package alarm.ruiqi.com.alarm_pro.options.alarm.UI;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.Toast;

import com.daimajia.androidanimations.library.Techniques;
import com.daimajia.androidanimations.library.YoYo;
import com.github.clans.fab.FloatingActionButton;
import com.github.clans.fab.FloatingActionMenu;
import com.lidroid.xutils.util.LogUtils;
import com.nineoldandroids.animation.Animator;
import com.nineoldandroids.animation.AnimatorListenerAdapter;

import alarm.ruiqi.com.alarm_pro.R;
import alarm.ruiqi.com.alarm_pro.options.MainActivity;
import alarm.ruiqi.com.alarm_pro.options.alarm.adapter.AlarmListAdapter;

/**
 * Created by 徐宇健 on 2015/8/7 0007.
 */
public class AlarmUIHelper {

    private  static  AlarmUIHelper instance;
    private AlarmUIHelper(){};
    public  static AlarmUIHelper getInstance(){
        if (instance == null)
            instance = new AlarmUIHelper();
        return instance;
    }


    //region 显示或关闭编辑闹钟layout
    //======================显示或关闭编辑闹钟layout  ==================================
    public  void showEditLayout(boolean flag,MainActivity activity, final AlarmListAdapter adapter,FloatingActionButton button, final FloatingActionMenu menu){
        if (flag)
            show(activity,adapter,button,menu);
        else
            dismiss(activity,adapter,button,menu);
    }

    private  void show(MainActivity activity, final AlarmListAdapter adapter,FloatingActionButton button, final FloatingActionMenu menu) {
        LogUtils.i("show layout");
        adapter.showEditLayout();
        final CheckBox checkBox = (CheckBox) LayoutInflater.from(activity).inflate(R.layout.default_checkbox,null);
        checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (checkBox.isChecked()) {
                    adapter.checkAllSelect();
                } else {
                    adapter.cancelAllSelect();
                }
            }
        });
        YoYo.with(Techniques.ZoomOutDown).playOn(button);
        YoYo.with(Techniques.SlideInUp).withListener(new MyAnimListener(true) {
            @Override
            public void onAnimationStart(Animator animation) {
                menu.setVisibility(View.VISIBLE);
                super.onAnimationStart(animation);
            }
        }).playOn(menu);
        activity.getHeadFragment().addRightView(checkBox);
    }


    class  MyAnimListener extends AnimatorListenerAdapter {
        private  boolean flag;
        public MyAnimListener( boolean flag){
            this.flag = flag;
        }
    }

    private  void dismiss(MainActivity activity, final AlarmListAdapter adapter,FloatingActionButton button, final FloatingActionMenu menu) {
        LogUtils.i("dismiss layout");
        menu.close(true);
        YoYo.with(Techniques.SlideInUp).playOn(button);
        YoYo.with(Techniques.ZoomOutDown).withListener(new MyAnimListener(true) {
            @Override
            public void onAnimationStart(Animator animation) {
                menu.setVisibility(View.VISIBLE);
                super.onAnimationStart(animation);
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                menu.setVisibility(View.GONE);
            }
        }).playOn(menu);
        adapter.closeEditLayout();
        activity.getHeadFragment().removeRightView();
        activity.getHeadFragment().setHeadText(null);
    }
    //======================显示或关闭编辑闹钟layout  ==================================
    //endregion

    //region 打开或关闭音乐
    //======================打开或关闭音乐  ==================================

    public void playMusic(Context context,MediaPlayer player,Uri pickUri,ImageButton palyButton) {
        if (TextUtils.isEmpty(pickUri.toString())) return;
        palyButton.setBackgroundResource(R.mipmap.videoplayer_icon_stop_highlighted);
        try {
            player.setDataSource(context, pickUri);
        } catch (Exception e) {
            e.printStackTrace();
        }
        final AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) == 0) {
            Toast.makeText(context, "当前闹钟音量静音...", Toast.LENGTH_SHORT).show();
            audioManager.setStreamVolume(AudioManager.STREAM_ALARM, audioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM) / 2, 0);
        }
        player.setAudioStreamType(AudioManager.STREAM_ALARM);
        player.setLooping(true);
        try {
            player.prepare();
        } catch (Exception e) {
            e.printStackTrace();
        }
        player.start();
    }
    public void stopMusic(MediaPlayer player,ImageButton palyButton) {
        if (player == null) return;
        palyButton.setBackgroundResource(R.mipmap.videoplayer_icon_play_highlighted);
        player.stop();
    }
    //======================打开或关闭音乐  ==================================
    //endregion



}
